#include "wtlpch.h"

#include "resource.h"
#include "TARegisterHelper.h"

CTARegisterHelper::CTARegisterHelper(void)
{
}

CTARegisterHelper::~CTARegisterHelper(void)
{
}

HICON CTARegisterHelper::RegGetIconFromExtension(wstring& wstrDotExt)
{
	//Prepare buffer to retire value
	WCHAR lpwstrRegValue[MAX_REGISTER_VALUE_BUFFER] = { 0 };
	//We use WCHAR, so the byte size multi 2
	DWORD dwByteBufferSize = MAX_REGISTER_VALUE_BUFFER*2;
	//The Return value type, we do not care about this
	DWORD dwRetValueType = 0;
	//UINT return icon cout
	UINT uiIconCount = 0;

	//Prepare for the unknown type
	HICON hiconUnknownType = NULL;
	HICON hiconFileType = NULL;

	HKEY hKeyFileExt = NULL;
	if(::RegOpenKeyExW(HKEY_CLASSES_ROOT, wstrDotExt.c_str(), NULL, KEY_READ, &hKeyFileExt) != ERROR_SUCCESS)
	{
		//Infact, In w7 or Vista, we could choose the key \\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\.abc\\UserChoice  ProgID
		//For a icon, but there is not much docs about this, so I just supress here

		//We did not find the specified extension, so we return the default one;
		hiconUnknownType = ::LoadIconW(::GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_UNKNOWNTYPE));
		return hiconUnknownType;
	}

	//Get the default value
	::RegQueryValueExW(hKeyFileExt, NULL, NULL, &dwRetValueType, (LPBYTE)lpwstrRegValue, &dwByteBufferSize);
	::RegCloseKey(hKeyFileExt);

	//Generate the default Icon Key
	wstring wstrDefIcon = lpwstrRegValue;
	wstrDefIcon += L"\\DefaultIcon";

	hKeyFileExt = NULL;
	if(::RegOpenKeyExW(HKEY_CLASSES_ROOT, wstrDefIcon.c_str(), NULL, KEY_READ, &hKeyFileExt) != ERROR_SUCCESS)
	{
		//We did not find that one, so we return the default one;
		hiconUnknownType = ::LoadIconW(::GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_UNKNOWNTYPE));
		return hiconUnknownType;
	}

	//Reinit the icon size
	dwByteBufferSize = MAX_REGISTER_VALUE_BUFFER*2;
	::wmemset(lpwstrRegValue, 0, MAX_REGISTER_VALUE_BUFFER);

	//Get the Icon Path and index
	::RegQueryValueExW(hKeyFileExt, NULL, NULL, &dwRetValueType, (LPBYTE)lpwstrRegValue, &dwByteBufferSize);
	::RegCloseKey(hKeyFileExt);

	//Extract File Path and icon index from regvalue
	WCHAR* pwcPos = lpwstrRegValue;
	for(; *pwcPos != L'\0'; pwcPos++)
	{
		//there is a semi between the file path and icon index
		if(*pwcPos == L',')
		{
			//We split it into two null term string
			*pwcPos = L'\0';
			pwcPos++;
			break;
		}
	}

	//Handle the path, remove quote, blank or single quote if there is
	LPWSTR lpwstrTrims = L"\"\' ";
	::StrTrimW(lpwstrRegValue, lpwstrTrims);

	INT nIndexIcon = ::_wtoi(pwcPos);

	//We just extract a smal icon
	uiIconCount = ::ExtractIconExW(lpwstrRegValue, nIndexIcon, NULL, &hiconFileType, 1);

	//if We succesffuly extract one icon
	if(uiIconCount == 1)
	{
		return hiconFileType;
	}
	else	//failed to extract, so we return the default unknown one
	{
		hiconUnknownType = ::LoadIconW(::GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_UNKNOWNTYPE));
		return hiconUnknownType;
	}
}

wstring CTARegisterHelper::ExtractExtFromFileName(wstring& wstrFileName)
{
	wstring wstrFileExt = L".TA_UNKNOW";
	//Position for the dot at reverse
	UINT uiRevPos = 1;

	for(wstring::iterator it = --wstrFileName.end(); it != wstrFileName.begin(); --it, uiRevPos++)
	{
		if(*it == L'.')
		{
			break;
		}
	}

	//We did not find the dot .
	if(uiRevPos == wstrFileName.length() || uiRevPos == 1)
	{
		return wstrFileExt;
	}

	wstrFileExt = wstrFileName.substr(wstrFileName.length() - uiRevPos);

	return wstrFileExt;
}
